<?php
abstract class DomainObject {
 
	protected $_Validator;
	
	/**
	 * holds an array of validation messages that may have occurred.
	 */
	protected $_validation_messages = array();
	
	
    function markDirty() {
        ObjectManager::markDirty( $this );
    }

    function finder() {
        return self::getFinder( get_class( $this ) );
    }
    
	/**
	 * expects a validator that can be used to validate this object
	 *
	 * @param Validator $validator
	 */
	public function setValidator(Validator $validator=null)
	{
		$this->_Validator = $validator;
	}
	
	/**
	 * returns the validator set on the object
	 *
	 * @return Validator $validator
	 */
	public function getValidator()
	{
		return $this->_Validator;
	}
    
    /**
     * use the objects validator to determine if the object is valid or not based on the current context
     *
     * @return unknown
     */
    function isValid() {
    	// set a default validator, this can be overridden with the setValidator depending on your context
		if(is_null($this->_Validator)) {
			$this->_Validator = $this->getDefaultValidator();
		}
		
		if(!is_null($this->_Validator)) {
		
			$this->_validation_messages =  $this->_Validator->validate($this);
			if(count($this->_validation_messages) > 0) {
				return false;
			}
			return true;
		} 
    }
    
    /**
     * returns an array of validation messages
     *
     * @return unknown
     */
    function getMessages()
    {
    	return $this->_validation_messages;
    }

    static function getFinder( $type ) {
    	
        return MapperFactory::getMapper( $type ); 
    }
    
    
	public function __set($name, $value)
	{
		$this->$name = $value;
	}
	
	public function __get($name)
	{
		return $this->$name;
	}
	
    
    
}
?>